/**
 * 
 */
package cn.haona.game.simb.wap.servlet.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import cn.haona.game.simb.object.bio.User;
import cn.haona.game.simb.servlet.Define;
import cn.haona.game.simb.servlet.SessionHn;
import cn.haona.game.simb.wap.servlet.WebDefine;

import com.opensymphony.xwork2.ActionContext;

/**
 * 用户操作过滤器
 * @author shenhao
 *
 */
public class UserOperateFilter implements Filter
{

	/**
	 * 
	 */
	public UserOperateFilter()
	{
		// TODO Auto-generated constructor stub
	}
	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig arg0) throws ServletException
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy()
	{
		// TODO Auto-generated method stub

	}

	/* 
	 * 过滤一下
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException
	{	
		//用户
		User user=getUser(request);
		//如果不为空,执行用户操作进入之前的逻辑
		if(user!=null)
		{
			
		}
		
		//生成用户操作KEY
		String operateKey=String.valueOf(System.currentTimeMillis()%1000);
		//操作KEY保存到request中
		request.setAttribute(WebDefine.OPERATE_KEY, operateKey);
		
		
		//继续穿越下面的过滤器
		chain.doFilter(request, response);
		
		//如果用户为空,那么再次找到用户对象
		if(user==null)
		{
			user=getUser(request);
			//如果不为空,执行用户操作进入之后的逻辑
			if(user!=null)
			{
				
			}
		}
		
		//用户操作KEY放入会话Bean中
		SessionHn session=(SessionHn)request.getAttribute(Define.SESSION_ID);
		session.addElement(SessionHn.KEY_OPERATEID, operateKey);
		System.out.println("---UserOperateFilter---");
		
	}
	
	/**
	 * 得到用户对象
	 * @param request
	 * @return
	 */
	private User getUser(ServletRequest request)
	{
		//用户
		User user=null;
		SessionHn session = (SessionHn) request.getAttribute(Define.SESSION_ID);

		if (session != null)
		{
			// 取的User object
			user = (User) session.getElement(SessionHn.KEY_USERBEAN);
		}
		return user;
	}
	


}
